package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.starseas_phone.Adapters.AddEvaluationlistviewAdapter;
import com.example.starseas_phone.Adapters.Stu_EvaluationlistviewAdapter;
import com.example.starseas_phone.Entity.AddEvaluation;
import com.example.starseas_phone.Entity.Evaluation;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class AddevaluationActivity extends AppCompatActivity {

    private FloatingActionButton add_evaluation_floatbtn;

    private ListView add_evaluation_list;
    private AddEvaluationlistviewAdapter adapter;
    private List<AddEvaluation> list1 = new ArrayList<>();
    private List<AddEvaluation> list = new ArrayList<>();
    private List<Integer> lilist = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addevaluation);
        initls();
        initid();

        add_evaluation_floatbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AddevaluationActivity.this,Stu_evaluationActivity.class);
                intent.putExtra("id","1");
                startActivity(intent);
            }
        });

        add_evaluation_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                AddEvaluation addEvaluation = adapter.getItem(i);
                Intent intent = new Intent(AddevaluationActivity.this,MarkActivity.class);
                intent.putExtra("addEvaluation",addEvaluation);
                startActivity(intent);
            }
        });

    }

    private void initid(){
        add_evaluation_floatbtn = (FloatingActionButton) findViewById(R.id.add_evaluation_floatbtn);
        add_evaluation_list = (ListView) findViewById(R.id.add_evaluation_list);
        adapter = new AddEvaluationlistviewAdapter(AddevaluationActivity.this,R.layout.add_evaluation_listview,list);

    }

    private void initalllist(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/Schedule/GetWorkerEvaluationByStudent?token="+Sto.students.getToken()+"&Semester=4")
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    JSONArray jsonArray = new JSONArray(msg);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObjects = jsonArray.getJSONObject(i);
                        String SchoolSemester = jsonObjects.getString("SchoolSemester");
                        int CourseId= jsonObjects.getInt("CourseId");
                        String CourseName = jsonObjects.getString("CourseName");
                        int CollegeId= jsonObjects.getInt("CollegeId");
                        String WorkerId=jsonObjects.getString("WorkerId");
                        String WorkerName=jsonObjects.getString("WorkerName");
                        String ClassId=jsonObjects.getString("ClassId");
                        String ScheduleWeekly = jsonObjects.getString("ScheduleWeekly");
                        String ScheduleWeek= jsonObjects.getString("ScheduleWeek");
                        String ScheduleWhat=jsonObjects.getString("ScheduleWhat");
                        String SchedulePlace=jsonObjects.getString("SchedulePlace");
                        int Semester=jsonObjects.getInt("Semester");
                        AddEvaluation addEvaluation = new  AddEvaluation(SchoolSemester,CourseId,CourseName,CollegeId,WorkerId,WorkerName,ClassId,ScheduleWeekly,ScheduleWeek,ScheduleWhat,SchedulePlace,Semester);
                        list1.add(addEvaluation);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            for (int i = 0; i < list1.size(); i++) {
                                int x = 0;
                                Log.d("AddevaluationActivitylist1",list1.get(i).getCourseId()+"");
                                for (int j = 0; j < lilist.size(); j++) {
                                    Log.d("lilist",lilist.get(j)+"");
                                    if (list1.get(i).getCourseId()==lilist.get(j)){
                                        x=1;
                                        break;
                                    }
                                }
                                if (x==0){
                                    list.add(list1.get(i));
                                }
                            }
                            adapter.notifyDataSetChanged();
                            add_evaluation_list.setAdapter(adapter);
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initls(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/WorkerEvaluation/GetWorkerEvaluation?token="+Sto.students.getToken())
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    JSONArray jsonArray = new JSONArray(msg);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObjects = jsonArray.getJSONObject(i);
                        int CourseId= jsonObjects.getInt("CourseId");
                        lilist.add(CourseId);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            initalllist();
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }


    @Override
    protected void onRestart() {
        list.clear();
        lilist.clear();
        list1.clear();
        initls();
        super.onRestart();
    }
}